package ahblue.cn.tanzi.ui.home.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.data.Result;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.model.SearchWord;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity {

    @BindView(R.id.rl_search_history)
    RelativeLayout mRelativeLayoutSearchHistory;

    @BindView(R.id.flowlayout)
    TagFlowLayout mTagFlowLayoutSearchHistory;
    @BindView(R.id.flowlayout2)
    TagFlowLayout mTagFlowLayoutSearchHot;

    @BindView(R.id.edit_search)
    EditText mEditTextSearch;

    private LayoutInflater mInflater;
    private List<SearchWord> mSearchedWordList;
    private List<SearchWord> mSearchedHistoryWordList;

    private LoginResultModel mLoginResultModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_label);

        mInflater = LayoutInflater.from(this);

        ButterKnife.bind(this);

        initData();
        initView();
    }

    private void initData() {
        mLoginResultModel = LoginResultModel.getUserModel(this, LoginResultModel.class);

        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.getBigShotHotWord(mHotWordCallback);

        repo.getBigShotHistoryList(mLoginResultModel.token, mSearchHistoryCallback);
    }

    private void initView() {
        mRelativeLayoutSearchHistory.setVisibility(View.GONE);

        mTagFlowLayoutSearchHot.setOnTagClickListener(mOnHotSearchTagClickListener);
        mTagFlowLayoutSearchHistory.setOnTagClickListener(mOnHistorySearchTagClickListener);

        mEditTextSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String word = mEditTextSearch.getText().toString();
                    if (word.length() == 0)
                        return false;

                    goSearch(word);
                    return true;
                }

                return false;
            }
        });
    }

    @OnClick(R.id.tv_search_cancle)
    void onClickCancle() {
        finish();
    }

    // 清除搜索历史
    @OnClick(R.id.tv_search_label_clear)
    void onClickClearSearchHistory() {
        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.getBigShotHistoryDelete(mLoginResultModel.token, new ApiCallback<Result>() {
            @Override
            public void onSuccess(Result data) {
                mSearchedHistoryWordList.clear();
                mTagFlowLayoutSearchHistory.getAdapter().notifyDataChanged();
            }

            @Override
            public void onFailed(String errorMsg) {

            }
        });
    }

    private ApiCallback<List<SearchWord>> mHotWordCallback = new ApiCallback<List<SearchWord>> () {

        @Override
        public void onSuccess(List<SearchWord> data) {
            mSearchedWordList = data;
            HotSearchTagAdapter adapter = new HotSearchTagAdapter(data);
            mTagFlowLayoutSearchHot.setAdapter(adapter);
        }

        @Override
        public void onFailed(String errorMsg) {

        }
    };

    private ApiCallback<List<SearchWord>> mSearchHistoryCallback = new ApiCallback<List<SearchWord>>() {
        @Override
        public void onSuccess(List<SearchWord> data) {
            if (data.size() == 0)
                return;

            mSearchedHistoryWordList = data;
            HistorySearchTagAdapter adapter = new HistorySearchTagAdapter(mSearchedHistoryWordList);
            mTagFlowLayoutSearchHistory.setAdapter(adapter);

            mRelativeLayoutSearchHistory.setVisibility(View.VISIBLE);
        }

        @Override
        public void onFailed(String errorMsg) {

        }
    };

    class HotSearchTagAdapter extends TagAdapter<SearchWord> {

        HotSearchTagAdapter(List<SearchWord> datas) {
            super(datas);
        }

        @Override
        public View getView(FlowLayout parent, int position, SearchWord searchWord) {
            TextView view = (TextView) mInflater.inflate(R.layout.layout_tag, mTagFlowLayoutSearchHot, false);
            view.setText(searchWord.name);

            return view;
        }
    }

    class HistorySearchTagAdapter extends TagAdapter<SearchWord> {

        HistorySearchTagAdapter(List<SearchWord> datas) {
            super(datas);
        }

        @Override
        public View getView(FlowLayout parent, int position, SearchWord searchWord) {
            TextView view = (TextView) mInflater.inflate(R.layout.layout_tag, mTagFlowLayoutSearchHistory, false);
            view.setText(searchWord.name);

            return view;
        }
    }

    TagFlowLayout.OnTagClickListener mOnHotSearchTagClickListener = new TagFlowLayout.OnTagClickListener() {
        @Override
        public boolean onTagClick(View view, int position, FlowLayout parent) {
            SearchWord word = mSearchedWordList.get(position);
            goSearch(word.name);

            return true;
        }
    };

    TagFlowLayout.OnTagClickListener mOnHistorySearchTagClickListener = new TagFlowLayout.OnTagClickListener() {
        @Override
        public boolean onTagClick(View view, int position, FlowLayout parent) {
            SearchWord word = mSearchedHistoryWordList.get(position);
            goSearch(word.name);

            return true;
        }
    };

    private void goSearch(@NonNull String word) {
        if (word.length() == 0)
            return;

        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.addBigShotAddWord(mLoginResultModel.token, word, null);

        final Intent intent = new Intent(this, SearchResultActivity.class);
        intent.putExtra(SearchResultActivity.ARG_SEARCH_WORD, word);
        startActivity(intent);
    }
}
